﻿/*-------------------------------------------------------------------------
* CLR版本:     4.0.30319.42000
* 创建人:      song 
* 创建时间：   2024/7/17 15:29:56
* 功 能：      proto 序列化辅助
*-------------------------------------------------------------------------*/

using ProtoBuf;
using System.IO;

namespace BattleCore.utils
{
    public class util_serialize
    {
        public static byte[] ProtoSerialize<T>(T record) where T : class
        {
            if (null == record) return null;

            try
            {
                using (var stream = new MemoryStream())
                {
                    Serializer.Serialize(stream, record);
                    return stream.ToArray();
                }
            }
            catch
            {
                // Log error
                throw;
            }
        }
        public static T ProtoDeserialize<T>(byte[] data) where T : class
        {
            if (null == data) return null;

            try
            {
                using (var stream = new MemoryStream(data))
                {
                    return Serializer.Deserialize<T>(stream);
                }
            }
            catch
            {
                // Log error
                throw;
            }
        }
    }
}